package com.greenantmall.activity;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.greenantmall.R;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.utils.iLog;
import com.lsarah.utils.SystemBarTintManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MyInfoActivity extends BaseActivity {

    private EditText name;
   // private EditText mobile;
  //  private EditText email;
    private EditText idnumber;
    private TextView modify;
    private TextView iv_title;
    private Context mContext;
    
    private final static String realname="realname";
    private final static String IDNumber="realAddress";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        
        SystemBarTintManager.setContentView(this, R.layout.myinfo, getResources().getColor(R.color.green));
        name = (EditText) this.findViewById(R.id.name);
       
        idnumber = (EditText) this.findViewById(R.id.info_idnumber);
        modify = (TextView) this.findViewById(R.id.modify);
        final  AVUser current = AVUser.getCurrentUser();
        name.setText(current.getString(realname));
     
        idnumber.setText(current.getString(IDNumber));
        
        modify.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                if (!checkIfIsCorrect())
                {
                    UtilTools.showMyToast(mContext, mContext.getString(R.string.failed));
                    return;
                }
                current.put(realname, name.getText().toString());
                current.put(IDNumber, idnumber.getText().toString());
                current.saveInBackground(new SaveCallback()
                {
                    @Override
                    public void done(AVException arg0)
                    {
                      if(arg0==null)
                      {
                          UtilTools.showMyToast(mContext, mContext.getString(R.string.success));  
                      }else{
                         int code= arg0.getCode();
                         if(code==125)
                         {
                             UtilTools.showMyToast(mContext, mContext.getString(R.string.error_register_email_invalid)); 
                         }else if(code==203)
                         {
                             UtilTools.showMyToast(mContext, mContext.getString(R.string.error_register_email_repeat));    
                         }else{
                        	 UtilTools.showMyToast(mContext, mContext.getString(R.string.failed)); 
                         }
                          iLog.d("getLocalizedMessage:"+ arg0.getLocalizedMessage()+" "+arg0.getMessage());
                      }
                    }
                });   
            }
        });
    }
    public void goBack(View view) {
        finish();
    }
    public void addressManage(View view) {
      Intent intent = new Intent(this, MyAddressActivity.class);
      startActivity(intent);
    }
    
    
    private boolean checkIfIsCorrect()
    {
        if (TextUtils.isEmpty(name.getText().toString()) 
            || TextUtils.isEmpty(idnumber.getText().toString()))
        {
            return false;
        }
        return true;
    }

}
